﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;


namespace IHISCETeim.Web.MachineSys.JSDWF
{
    public partial class JSDWF_ApplyDWF :PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.none };
            }
        }
        protected override string DWZRef
        {
            get { return "JSDWFList"; }
        }

        /// <summary>
        /// 需审批的申请
        /// </summary>
        protected Guid ApplyGUID
        {
            get
            {
                string g = GetRequestValue<string>("g", "");
                if (g == "")
                    throw new Exception("服务器不接受您的请求.");
                return new Guid(g);
            }
        }
        /// <summary>
        /// 审批结果状态
        /// </summary>
        protected int ApplyState
        {
            get
            {
                int g = GetRequestValue<int>("s", 0);
                if (g == 0)
                    throw new Exception("服务器不接受您的请求.");
                return g;
            }
        }

        protected string Remark
        {
            get
            {
                return "";
            }
        }

        private JSDWFApplyInfo _applyObj = null;
        protected JSDWFApplyInfo applyObj
        {
            get
            {
                if (_applyObj == null)
                    _applyObj = JSDWFApplyController.GetByApplyGUID(ApplyGUID);
                return _applyObj;
            }
        }

        private OUserInfo _ApplyUser = null;
        protected OUserInfo ApplyUser
        {
            get
            {
                if (_ApplyUser == null)
                    _ApplyUser = OUserController.GetByUserName(applyObj.ApplyUserName);
                return _ApplyUser;
            }
        }


        private OUserInfo _CurrFlowUser = null;
        protected OUserInfo CurrFlowUser
        {
            get
            {
                if (_CurrFlowUser == null)
                {
                    // OUserInfo ApplyUser = OUserController.GetByUserName(itm.ApplyUserName);//申请人员
                    JSDWFFlowConfigInfo flowObj = JSDWFFlowConfigController.GetByFlowId(applyObj.FlowID,applyObj.AdtlvlID);
                    _CurrFlowUser = OUserController.GetByUserName(flowObj.UserName);//审批人员
                }
                return _CurrFlowUser;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.ltmsg.Text = proccess();
        }

        private string proccess()
        {
            string rmsg = "";
            //1.判断当前用户是否与审批人员相关
            if (base.User.UserName != CurrFlowUser.UserName)
            {
                rmsg = "提示:此申请无需你进行审批.";
                return rmsg;
            }

            if(applyObj.ApplyState!=0)
            {
                rmsg = "提示:此申请您已审批过,无需再进行审批.";
                return rmsg;
            }
            

            //2.状态判断,如果为拒绝
            if (ApplyState == 2)
            {
                JSDWFApplyController.CompleteFlowReject(ApplyGUID, Remark);
                rmsg = string.Format("提示:您已拒绝{0}的图纸申请.", ApplyUser.RealName);
                // return;
            }

            //3.如果为通过

            if (ApplyState == 1)
            {
                //3.1是否有下一个审批者
                JSDWFFlowConfigInfo nextobj = JSDWFFlowConfigController.GetNextFlow(applyObj.FlowID, applyObj.AdtlvlID);
                if (nextobj == null)//没有一下个审批者，完成审批
                {
                    JSDWFApplyController.CompleteFlowPass(ApplyGUID);
                    rmsg = string.Format("提示:{0}的图纸申请已审批完成.", ApplyUser.RealName);
                }
                else
                {
                    JSDWFApplyController.UpdateNextFlow(ApplyGUID, nextobj.FlowID);
                    rmsg = string.Format("提示:{0}的图纸申请已通过审批.", ApplyUser.RealName);
                }
                //  return;
            }

            return rmsg;
        }
    }
}